package cn.hesion.rocketmqdemo.cn.hesion.producer;
/**
 * ClassName: MQProvider <br/>
 * Description: <br/>
 * date: 2021/11/5 10:53<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RocketMQ生产者
 * @author: hesion
 * @create: 2021-11-05 10:53
 **/
@Getter
@Setter
@ToString
@Configuration
@Slf4j
@ConfigurationProperties(prefix = "rocketmq.producer")
public class MQProvider {

    private String groupName;

    private String namesrvAddr;
    // 消息最大值
    private Integer maxMessageSize;
    // 消息发送超时时间
    private Integer sendMsgTimeOut;
    // 失败重试次数
    private Integer retryTimesWhenSendFailed;

    @Bean
    @ConditionalOnProperty(prefix = "rocketmq.producer", value = "isOnOff",havingValue = "on")
    public DefaultMQProducer defaultMQProducer() throws MQClientException {
        log.info("defaultProducer 正在创建---------------------------------------");
        DefaultMQProducer producer = new DefaultMQProducer(groupName);
        producer.setNamesrvAddr(namesrvAddr);
        producer.setVipChannelEnabled(false);
        producer.setMaxMessageSize(maxMessageSize);
        producer.setSendMsgTimeout(sendMsgTimeOut);
        producer.setRetryTimesWhenSendAsyncFailed(retryTimesWhenSendFailed);
        producer.start();
        log.info("rocketmq producer server 启动--------------------------------");
        return producer;
    }
}
